﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZHDJ.Core.Util;
using ZHDJ.Data.Models.SMS;

namespace ZHDJ.Data.BLL
{
    public class SMSHelper
    {
        /// <summary>
        /// 创建短信发送记录
        /// </summary>
        /// <param name="mobileNo">手机号码</param>
        /// <param name="smsContent">短信内容(最大长度100)</param>
        /// <param name="source">短信来源（可以填写模块标识或空字符）</param>
        /// <returns></returns>
        public static bool CreateSmsSendRecord(string mobileNo, string smsContent, string source = "")
        {
            if (smsContent != null && smsContent.Length > 100)
                smsContent = smsContent.Substring(0, 100);

            var para = new List<MySqlParameter>
            {
                new MySqlParameter() {DbType = DbType.String, ParameterName = "ID", Value = StringHelper.NewID },
                new MySqlParameter() {DbType = DbType.String, ParameterName = "Source", Value = source},
                new MySqlParameter() {DbType = DbType.String, ParameterName = "MobileNo", Value = mobileNo},
                new MySqlParameter() {DbType = DbType.String, ParameterName = "SMSContent", Value = smsContent},
                new MySqlParameter() {DbType = DbType.String, ParameterName = "MSGGroup", Value = StringHelper.NewID}
            };
            var sql = @" INSERT INTO tblPartySmsSendRecord (ID, Source, MobileNo, SMSContent, SMSPriority, 
	                        MSGGroup, IsMo, BatchNumber, CreateTime, SendState, SendTimes, SMSSubmitResult, SubmitReportResult, StateReportResult)
                         VALUES (?ID, ?Source, ?MobileNo, ?SMSContent, 1, 
	                        ?MSGGroup, 0, '', now(), '0', '0', '', '', '');";
            return MySqlHelper.ExecuteNonQuery(sql, para.ToArray()) > 0;
        }

        public static DataTable GetSmsSendRecordByNotSend()
        {
            return ZHDJ.Data.MySqlHelper.ExecuteDataTable("SELECT * FROM tblPartySmsSendRecord WHERE SendState = 0 ORDER BY CreateTime");
        }

        public static void UpdateSmsSendRecordSendState(string id, int sendResult)
        {
            string sql = string.Format(@"UPDATE tblPartySmsSendRecord SET SendTime = NOW(),SendState='{0}', 
                                            SendTimes=SendTimes+1,SMSSubmitResult = '{1}' WHERE ID = '{2}'",
                                         (sendResult == 1 ? 1 : -1), sendResult, id);

            ZHDJ.Data.MySqlHelper.ExecuteNonQuery(sql);
        }

        public static int UpdateSmsSendRecordSubmitReportsState(string msgGroup, string Mobile, string reportStatus)
        {
            string sql = string.Format(@"UPDATE tblPartySmsSendRecord SET SendState='{0}', 
                                            SubmitReportResult = '{1}' WHERE MSGGroup = '{2}' and MobileNo = '{3}'",
                                         (reportStatus == "CM:0000" ? 1 : -1), reportStatus, msgGroup, Mobile);

            return ZHDJ.Data.MySqlHelper.ExecuteNonQuery(sql);
        }

        public static int UpdateSmsSendRecordStatusReportsState(string msgGroup, string Mobile, string reportStatus)
        {
            string sql = string.Format(@"UPDATE tblPartySmsSendRecord SET SendState='{0}', 
                                            StateReportResult = '{1}' WHERE MSGGroup = '{2}' and MobileNo = '{3}'",
                                         (reportStatus == "CM:3000" ? 2 : -1), reportStatus, msgGroup, Mobile);

            return ZHDJ.Data.MySqlHelper.ExecuteNonQuery(sql);
        }

        public static void SaveSMSSubmitReport(SMSSubmitReport smsSubmitReport, int SmsNumber)
        {
            string sql = string.Format(@"INSERT INTO tblpartysmssubmitreport(ID,ReportStatus,Mobile,SubmitDate,
                                            ReceiveDate,ErrorCode,MsgGroup,CreteTime,Remark, SmsNumber)
                                        VALUES('{0}','{1}','{2}','{3}','{4}','{5}','{6}',Now(),'{7}', {8})",
                                        smsSubmitReport.ID, smsSubmitReport.ReportStatus, smsSubmitReport.Mobile, smsSubmitReport.SubmitDate,
                                        smsSubmitReport.ReceiveDate, smsSubmitReport.ErrorCode, smsSubmitReport.MsgGroup, smsSubmitReport.Remark, SmsNumber);

            ZHDJ.Data.MySqlHelper.ExecuteNonQuery(sql);
        }

        public static void SaveSMSStatusReport(SMSStatusReport smsStatusReport, int SmsNumber)
        {
            string sql = string.Format(@"INSERT INTO tblpartysmsstatusreport(ID,ReportStatus,Mobile,SubmitDate,
                                            ReceiveDate,ErrorCode,MsgGroup,CreteTime,Remark, SmsNumber)
                                        VALUES('{0}','{1}','{2}','{3}','{4}','{5}','{6}',Now(),'{7}', {8})",
                                        smsStatusReport.ID, smsStatusReport.ReportStatus, smsStatusReport.Mobile, smsStatusReport.SubmitDate,
                                        smsStatusReport.ReceiveDate, smsStatusReport.ErrorCode, smsStatusReport.MsgGroup, smsStatusReport.Remark, SmsNumber);

            ZHDJ.Data.MySqlHelper.ExecuteNonQuery(sql);
        }

        public static void SaveSMSMoreRecord(List<PartySmsmoRecord> recordlist) {
            StringBuilder sql = new StringBuilder();
            foreach (var item in recordlist)
            {
                sql.AppendFormat(@"INSERT INTO tblpartysmsmorecord(ID,Mobile,Content,
                                            SendTime,AddSerial,CreateTime,SmsState)
                                        VALUES('{0}','{1}','{2}','{3}','{4}',Now(),'{5}');",
                                        StringHelper.NewID, item.Mobile, item.Content, item.SendTime,
                                        item.AddSerial, 0);
            }
            ZHDJ.Data.MySqlHelper.ExecuteNonQuery(sql.ToString());
        }
    }
} 
